package com.cpa.admin.service;

import com.cpa.admin.base.BaseService;
import com.cpa.admin.mapper.ProductMapper;
import com.cpa.admin.mapper.UserMapper;
import com.cpa.admin.mapper.UserOrderMapper;
import com.cpa.admin.model.UserDo;
import com.cpa.admin.model.UserOrderDo;
import com.cpa.admin.vo.StatisticsMonthOrderVo;
import com.cpa.admin.vo.UserPageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalTime;
import java.util.Date;
import java.util.List;

/**
 * @author zhen.zhao01
 * @date 2018/11/10
 */
@Service
public class UserOrderService extends BaseService<UserOrderDo, UserOrderMapper> implements UserOrderMapper {

    private final UserMapper userMapper;

    @Autowired
    public UserOrderService(UserMapper userMapper) {
        this.userMapper = userMapper;
    }


    /**
     * 设置结算操作
     * @param userId
     * @param settleCount
     * @param type 1: 立即发布 2: 定时推送
     */
    @Transactional(rollbackFor = Exception.class)
    public void settle(String userId, Integer settleCount, Integer type){
        UserDo userDo = userMapper.selectByPrimaryKey(userId);
        if (userDo == null) {
            throw new RuntimeException("");
        }

        UserOrderDo toDayOrder = mapper.selectToDay(userId);

        // 如果今天有记录, 先删除
        if (toDayOrder != null) {
            // 删除之前的记录
            delete(toDayOrder);
        }

        // 创建订单
        UserOrderDo userOrderDo = new UserOrderDo();
        userOrderDo.setSettleCount(settleCount);

        if (type == 2) {
            if (LocalTime.now().isAfter(userDo.getSettleAmountPushTime())) {
                type = 1;
            }
        } else {
            userOrderDo.setUpdateTime(new Date());
        }


        userOrderDo.setSettleType(type);
        userOrderDo.setUserId(userId);
        userOrderDo.setProductId(userDo.getProductId());
        userOrderDo.setCreateBy("1");
        userOrderDo.setDeleteFlag(0);
        insertSelective(userOrderDo);
    }

    @Override
    public void deleteByUserId(String userId) {
        mapper.deleteByUserId(userId);
    }

    @Override
    public UserOrderDo selectToDay(String userId) {
        return mapper.selectToDay(userId);
    }

    @Override
    public List<StatisticsMonthOrderVo> statisticsMonthOrderVo() {
        return mapper.statisticsMonthOrderVo();
    }

    @Override
    public Integer countUserOrder(String userId) {
        return mapper.countUserOrder(userId);
    }

    @Override
    public Integer countUnCleanUserOrder(String userId) {
        return mapper.countUnCleanUserOrder(userId);
    }

    @Override
    public List<UserOrderDo> findUnSettleOrder() {
        return mapper.findUnSettleOrder();
    }

    @Override
    public void updateUnSettleButTimeOverOrder() {
        mapper.updateUnSettleButTimeOverOrder();
    }

    @Override
    public List<UserPageVo> findUserPageVoList(String userId) {
        return mapper.findUserPageVoList(userId);
    }

    @Override
    public List<StatisticsMonthOrderVo> statisticsUserMonthOrder(String userId) {
        return mapper.statisticsUserMonthOrder(userId);
    }

}
